package com.zzz.mylitho;

import static com.facebook.litho.widget.SnapUtil.SNAP_TO_CENTER;

import android.graphics.Color;
import android.widget.LinearLayout;
import com.facebook.litho.annotations.FromEvent;
import com.facebook.litho.annotations.OnEvent;
import com.facebook.litho.sections.Children;
import com.facebook.litho.sections.SectionContext;
import com.facebook.litho.sections.annotations.GroupSectionSpec;
import com.facebook.litho.sections.annotations.OnCreateChildren;
import com.facebook.litho.sections.common.DataDiffSection;
import com.facebook.litho.sections.common.RenderEvent;
import com.facebook.litho.sections.common.SingleComponentSection;
import com.facebook.litho.sections.widget.ListRecyclerConfiguration;
import com.facebook.litho.sections.widget.RecyclerCollectionComponent;
import com.facebook.litho.widget.ComponentRenderInfo;
import com.facebook.litho.widget.HorizontalScroll;
import com.facebook.litho.widget.RenderInfo;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaojh on date 2019/10/22
 */

@GroupSectionSpec
public class ListSectionSpec {
    
    @OnCreateChildren
    static Children onCreateChildren(final SectionContext c) {
        
        return Children.create()
            //2个child排列，上面的横向滑动，下面是竖向滑动
            .child(
                SingleComponentSection.create(c)
                    .component(RecyclerCollectionComponent.create(c)
                        .disablePTR(true)
                        .recyclerConfiguration(
                            ListRecyclerConfiguration
                                .create()
                                .orientation(LinearLayout.HORIZONTAL)
                                .build())
                        .section(
                            DataDiffSection.<Integer>create(c)
                                .data(generateData(10))
                                .renderEventHandler(ListSection.onRender(c))
                                .build())
                        .canMeasureRecycler(true))
                    .build())
            .child(DataDiffSection.<Integer>create(c)
                .data(generateData(15))
                .renderEventHandler(ListSection.onRender(c)))   //通过onRender()填入data
            .build();
        
            /*
            //DataDiffSection把item放入data
        return Children.create()
            .child(DataDiffSection.<Integer>create(c)
                .data(generateData(32))
                .renderEventHandler(ListSection.onRender(c)))
                .build();
        */
        
        /*
        //SingleComponentSection是单一的item种类
        Children.Builder builder = Children.create();
        
        for (int i = 0; i < 10; i++) {
            builder.child(
                SingleComponentSection.create(c)
                    .key(String.valueOf(i))
                    .component(ListItem.create(c)
                        .color(i % 2 == 0 ? Color.WHITE : Color.LTGRAY)
                        .title("big world")
                        .subtitle("small world")
                        .shadowRadius(10)
                        .build()));
        }
        return builder.build();*/
    }
    
    private static List<Integer> generateData(int count) {
        final List<Integer> data = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            data.add(i);
        }
        return data;
    }
    
    @OnEvent(RenderEvent.class)
    static RenderInfo onRender(final SectionContext c, @FromEvent Integer model) {
        
        return ComponentRenderInfo.create()
            .component(
                ListItem.create(c)
                    .color(model % 2 == 0 ? Color.WHITE : Color.LTGRAY)
                    .title(model + ". big world")
                    .subtitle("small world")
                    .shadowRadius(10)
                    .build())
            .build();
    }
}
